package com.card.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.card.R;
import com.card.adapter.MyFragmentPagerAdapter;
import com.card.db.entity.User;
import com.card.fragment.LeftFragment;
import com.card.fragment.RightFragment;
import com.card.utils.IFragmentCallback;

import java.util.ArrayList;
import java.util.List;

/**
 * 功能页面
 */
public class FunctionActivity extends AppCompatActivity implements LeftFragment.OnFragmentInteractionListener, RightFragment.OnFragmentInteractionListener, View.OnClickListener {
    //ViewPager2
    ViewPager2 viewPager2;

    String password;
    String telephone;
    int id;
    //左边的布局
    private LinearLayout leftLayout, rightLayout;

    //右边的布局
    private ImageView leftImage, rightImage, isCurrent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_function);
        //初始化底部导航栏
        initTabView();
        //初始化viewpager2
        initPager();
        //初始化用户数据
        initUserData();

    }


    void initUserData(){
        Intent intent = getIntent();
        password = intent.getStringExtra("password");
        telephone = intent.getStringExtra("telephone");
        id = intent.getIntExtra("id",100000);
    }
    /**
     * 初始化底部导航栏
     */
    public void initTabView() {
        //初始化布局 并绑定监听事件
        leftLayout = findViewById(R.id.tab_lv_find);
        leftLayout.setOnClickListener(this);
        rightLayout = findViewById(R.id.tab_lv_me);
        rightLayout.setOnClickListener(this);

        //找到图片
        leftImage = findViewById(R.id.tab_iv_find);
        rightImage = findViewById(R.id.tab_iv_me);

        //默认左边被选中
        leftImage.setSelected(true);

        //设置当前选中的状态
        isCurrent = leftImage;

    }

    /**
     * 初始化viewpager2
     */
    public void initPager() {
        //获取viewpager
        viewPager2 = findViewById(R.id.viewpager_function);
        //创建适配器
        MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), getLifecycle(), getFragmentList());
        //传入适配器
        viewPager2.setAdapter(adapter);

        viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                //换页同时改变底部导航栏
                changeTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }
        });
    }

    /**
     * 改变底部导航栏
     *
     * @param position
     */
    private void changeTab(int position) {
        //当前状态设为空
        isCurrent.setSelected(false);
        switch (position) {
            case R.id.tab_lv_find:
                //如果是点击事件同时要换页
                viewPager2.setCurrentItem(0);
            case 0:
                //设置选中
                leftImage.setSelected(true);
                isCurrent = leftImage;
                break;
            case R.id.tab_lv_me:
                viewPager2.setCurrentItem(1);
            case 1:
                rightImage.setSelected(true);
                isCurrent = rightImage;
                break;
        }
    }

    /**
     * 初始化fragment列表数据
     *
     * @return
     */
    public List<Fragment> getFragmentList() {
        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(LeftFragment.newInstance(null, null));
        RightFragment rightFragment = RightFragment.newInstance(null, null);
        rightFragment.setIFragmentCallback(new IFragmentCallback() {
            @Override
            public void sendMessageToActivity(String msg) {

            }

            @Override
            public User getMessageFromActivity() {
                User user = new User(telephone, password);
                user.setId(id);
                return user;
            }
        });
        fragmentList.add(rightFragment);
        return fragmentList;
    }

    @Override
    public void onFragmentInteraction(Uri uri) {


    }

    /**
     * 点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        changeTab(v.getId());
    }


}
